import { post } from "../../../utils/request";
import ResourceTreeNode from "../resource/tree-node";
import utils from "../../../utils/utils";
const env = import.meta.env;

const apiResource = {
  queryResourceListing(data?: object) {
    return post(
      env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/resource.listing",
      data
    );
  },
  getResource(resourceId: string) {
    return post(env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/resource.get", {
      resourceId: resourceId,
    });
  },
  updateResource(data?: object) {
    return post(
      env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/resource.update",
      data
    );
  },
  deleteResource(resourceId: string) {
    return post(
      env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/resource.delete",
      { resourceId }
    );
  },
  queryResourceChildrenListing(parentId: string) {
    return post(
      env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/resource.children.listing",
      {
        parentId,
      }
    );
  },
  /**
   *
   * @param parentId
   * @returns
   */
  async loadChilds(parentId: string): Promise<ResourceTreeNode[]> {
    let treeData = Array<ResourceTreeNode>();
    await apiResource
      .queryResourceChildrenListing(parentId)
      .then((response: any) => {
        if (response.data.code == 200) {
          if (response.data.data != "") {
            treeData = utils.jsonArray2TypescriptArray(response.data.data);
          }
          // console.log("loadChilds1", response.data.data);
          // console.log("loadChilds2", treeData);
        }
      })
      .catch((err: any) => {
        utils.errorNotification("数据处理错误", err);
      });

    return treeData;
  },
  queryAllResource() {
    return post(
      env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/resource.all",
      ""
    );
  },
};

export default apiResource;
